package com.wen.springcloud;

import java.util.concurrent.*;
import java.util.*;

public class DynamicThreadPoolExecutor extends ThreadPoolExecutor {

    public DynamicThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    /**
     * 获取线程池中所有线程的运行堆栈
     */
    public Map<String, StackTraceElement[]> getRunningThreadStackTraces() {
        Map<String, StackTraceElement[]> stackTraces = new HashMap<>();
        for (Thread thread : Thread.getAllStackTraces().keySet()) {
            if (thread.isAlive()) {
                stackTraces.put(thread.getName(), thread.getStackTrace());
            }
        }
        return stackTraces;
    }
    
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        System.out.println("Thread " + t.getName() + " is about to execute task.");
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        System.out.println("Task executed.");
    }
}